package com.jiang.readline.Service.Impl;


import com.jiang.readline.Service.FileReadCppService;
import com.jiang.readline.Utils.CountBlackLinesUtils;
import com.jiang.readline.Utils.CountCodeLinesUtils;
import com.jiang.readline.Utils.CountCommentsUtils;
import com.jiang.readline.Utils.CountLinesUtil;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
public class FileReadCppServiceImpl implements FileReadCppService {

    public  static  int  Filecount = 0 ;  //定义常态值
    public  static  int  Lines = 0 ;
    public  static  int  Code = 0 ;
    public  static  int  Comments = 0 ;
    public  static  int  Blanks = 0 ;

    @Override
    public String readFile(File file) {
        //判断参数中的file是否为文件夹
        if (file.isDirectory()) {
        //拿到文件夹下的所有文件
            File[] files = file.listFiles();
        //遍历文件夹下的所有文件
            for (File f : files) {
        //递归 继续找此文件夹下的文件
                 readFile(f);
            }
        } else {
        //如果此文件下没有文件，则判断后续是否为.c .cpp .cc 结尾的文件，并统计一系列
            String fileName = file.getName();
            String path = file.getPath();
            if (fileName.endsWith(".c") || fileName.endsWith(".cpp") || fileName.endsWith(".cc")) {
                Filecount++;
                Lines+= CountLinesUtil.CountLines(path);
                Code+= CountCodeLinesUtils.CodeLines(path);
                Comments+= CountCommentsUtils.CommentsLines(path);
                Blanks+= CountBlackLinesUtils.BlackLines(path);
            }
        }
        //      文件          总行数     代码       注释        留白
        return Filecount+","+Lines+","+Code+","+Comments+","+Blanks;

    }







}
